/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package multiversiontechnicaldocumentation;

import java.util.ArrayList;

/**
 * Multiversion technical document.
 * 
 * Command syntax:
 * <ul>
 * <li>VERSION ID -> ID TIME TITLE</li>
 * <li>LOGICALSECTION ID TITLE CONTENTMODE</li>
 * <li></li>
 * <li></li>
 * <li></li>
 * <li></li>
 * <li></li>
 * <li></li>
 * </ul>
 * 
 * A document version can be used not only to create an "update"
 * of the document, but to create also a "variation" (or "branch"). 
 * Logical sections (but not only) should be regarded as <i>reusable parts</i>  
 * in all document versions.
 *
 * @author VadimBaitoiu
 */
public class Document {

    public static final int MAX_DEPTH = 8;

    public static class Version {

        private long time;
        private String title;
        private LogicalSection root;

        public void setTime(long time) {
            this.time = time;
        }

        public long getTime() {
            return time;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }

        public LogicalSection getRoot() {
            return root;
        }

        public void setRoot(LogicalSection root) {
            this.root = root;
        }

    }

    /**
     * Logical section.
     *
     * A logical section can have either text either a list of child logical
     * sections.
     */
    public static class LogicalSection {

        private long ID;
        private String title;
        private boolean contentMode; // false COPY true REPLACE
        private Object content; // LogicalSection (child) or String
        private LogicalSection next;

        public long getID() {
            return ID;
        }

        public void setID(long ID) {
            this.ID = ID;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public boolean isContentMode() {
            return contentMode;
        }

        public void setContentMode(boolean contentMode) {
            this.contentMode = contentMode;
        }

        public Object getContent() {
            return content;
        }

        public void setContent(Object content) {
            this.content = content;
        }

        public LogicalSection getNext() {
            return next;
        }

        public void setNext(LogicalSection next) {
            this.next = next;
        }

        public void appendChild(LogicalSection logicalSection) throws DocumentStructureException {
            if (this.content == null) {
                this.content = logicalSection;
            } else if (this.content instanceof Document.LogicalSection) {
                Document.LogicalSection prevChild = (Document.LogicalSection) this.content;
                while (prevChild.getNext() != null) {
                    prevChild = prevChild.getNext();
                }
                prevChild.setNext(logicalSection);
            } else {
                throw new DocumentStructureException("Cannot append child logical section");
            }
        }
    }
    private String path;
    private ArrayList<Version> versionList;
    private Version currentVersion; //

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public ArrayList<Version> getVersionList() {
        return versionList;
    }

    public void setVersionList(ArrayList<Version> versionList) {
        this.versionList = versionList;
    }

    public Document() {
        this.versionList = new ArrayList<>();
    }

    public Version getMostRecentVersion() {
        if (!this.versionList.isEmpty()) {
            int index = this.versionList.size() - 1;
            return this.versionList.get(index);
        } else {
            return null;
        }
    }

    public Version getCurrentVersion() {
        return currentVersion;
    }

    public void setCurrentVersion(Version currentVersion) {
        this.currentVersion = currentVersion;
    }
    
    
}
